package com.bosch.in.rtp1.ept.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.bosch.in.rtp1.ept.action.form.LotStatusCriteriaForm;
import com.bosch.in.rtp1.ept.constant.CommonConstants;
import com.bosch.in.rtp1.ept.constant.FilterParameter;
import com.bosch.in.rtp1.ept.constant.HoldCodeType;
import com.bosch.in.rtp1.ept.constant.TableType;
import com.bosch.in.rtp1.ept.manager.DurationColorConManager;
import com.bosch.in.rtp1.ept.manager.FrozenColumnManager;
import com.bosch.in.rtp1.ept.manager.HoldCodeFilterManager;
import com.bosch.in.rtp1.ept.manager.LotStatusManager;
import com.bosch.in.rtp1.ept.model.dto.DurationColorCon;
import com.bosch.in.rtp1.ept.model.dto.HoldCodeFilter;
import com.bosch.in.rtp1.ept.model.dto.JqgridJsonData;
import com.bosch.in.rtp1.ept.model.dto.LotStatus;
import com.bosch.in.rtp1.ept.model.technical.Condition4LotStatus;
import com.bosch.in.rtp1.ept.util.ConverterUtility;
import com.bosch.in.rtp1.ept.util.ManagerFactory;
import com.bosch.in.rtp1.ept.util.SQLConditionBuilder;
import com.bosch.in.rtp1.ept.util.StringUtility;
 
/**
 * The action class acts like a controller which will delegate to LotStatus manager class to 
 * do functionalities and then format returned data in kind of client supporting  
 * @author TPR1HC
 *
 */
public class LotStatusAction extends MappingDispatchAction{
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
    private static final Logger LOGGER = Logger.getLogger(LotStatusAction.class);
    
    /**
     * report template file to export to excel
     */
    private static final String EXCEL_TEMPLATE_FILE = "templates/lotstatus_template.xls";
    
    /**
     * report designer file to export to pdf
     */
    private static final String PDF_DESIGNER_FILE = "templates/lotstatus.jasper";
    
    private static final String JSON_DATA = "jsonData";
    
    
    /**
	 * Response to ajax call from Jquery grid, it will delegate to Manager class
	 * to query LotStatus according to search criteria in request parameter.
	 * The returned list then will be transformed to JSON format in order to 
	 * display on client side.
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
     * @throws SQLException 
	 */
	public ActionForward displayLotStatus(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException 
			{
		LOGGER.debug("Begin LotStatusAction.displayLotStatus");
		
		// get HoldCodeFilterManager instance
    	HoldCodeFilterManager holdCodeFilterManager = ManagerFactory.getInstance().getHoldCodeFilterManager();
    	
    	/* query all HoldCode filter values */
    	List<HoldCodeFilter> holdCodeFilters = holdCodeFilterManager.getHoldCodeFilters(HoldCodeType.HC);
    	((LotStatusCriteriaForm)form).setHoldCodeFilters(holdCodeFilters);
    	
    	// get FrozenColumnManager instance
    	FrozenColumnManager frozenColumnManager =  ManagerFactory.getInstance().getFrozenColumnManager();
    	List<String> frzCols = frozenColumnManager.getFrozenColumns(TableType.LOT_STATUS);
    	
    	// create duration color condition manager instance
		DurationColorConManager durColConManager = ManagerFactory.getInstance().getDurationColorConManager();
		
		// load durationColorCondition conditions
		DurationColorCon durColCon = durColConManager.getDuratonColCon();
		
    	request.setAttribute(CommonConstants.LOT_STATUS_FRZ_COLS, frzCols);
    	request.setAttribute(CommonConstants.RED_COLOR_CON, StringUtility.buildRedColorCondition(durColCon));
    	request.setAttribute(CommonConstants.YELLOW_COLOR_CON, StringUtility.buildYellowColorCondition(durColCon));
    	
    	LOGGER.debug("End LotStatusAction.displayLotStatus");
    	
    	// Forward to form page
        return mapping.findForward("success");
	}
    
    
	/**
	 * Response to ajax call from Jquery grid, it will delegate to Manager class
	 * to query LotStatus according to search criteria in request parameter.
	 * The returned list then will be transformed to JSON format in order to 
	 * display on client side.
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public ActionForward search(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException 
			{
		LOGGER.debug("Begin LotStatusAction.search");
		// build search condition from parameters in request
		Condition4LotStatus searchCriteria = SQLConditionBuilder.extractLotStatusParameter(request);
    	// get LotStatusManager instance
    	LotStatusManager lotStatusManager = ManagerFactory.getInstance().getLotStatusManager();
    	
    	/* query REAL-TIME data (DONT GET FROM CACHE) with search condition gotten from parameter in request */
    	List<LotStatus> lotStatuses = lotStatusManager.getLotStatus(searchCriteria);
    	
    	/* total records */
    	int totalRecords = lotStatuses.size();
    	
    	// convert returned data in kind of JSON format which Jqgrid supports
    	JSONArray cellarray = ConverterUtility.convertToJSONRows(lotStatuses);
    	JqgridJsonData jLotStatuses = new JqgridJsonData();
    	jLotStatuses.setPage(Integer.valueOf(request.getParameter(FilterParameter.PAGE)));
    	jLotStatuses.setRecords(totalRecords);
    	jLotStatuses.setRowNum(Integer.valueOf(request.getParameter(FilterParameter.ROW_NUM)));
    	jLotStatuses.setRows(cellarray);
    	
    	// write data to output stream with MIME type is text
    	//JSONSerializer serializer = new JSONSerializer();  
    	String jsonResponse = JSONSerializer.toJSON(jLotStatuses).toString();
    	response.setContentType("text/html;charset=utf-8");
    	response.setHeader("cache-control", "no-cache");
    	PrintWriter out = response.getWriter();
    	out.println(jsonResponse);
    	out.flush();
    	LOGGER.debug("End LotStatusAction.search");
    	return null;
	}
	
	/**
	 * Response to export to excel click, it will delegate to Manager class
	 * to query LotStatus according to search criteria in request parameter.
	 * The result list then will be save to excel file and returns to client
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 * @throws IOException 
	 * @throws ParseException 
	 */
	public ActionForward exportExcel(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException, ParseException 
			{
		LOGGER.debug("Begin LotStatusAction.exportExcel");
		
		String jsonLotStatuses = request.getParameter(JSON_DATA);
		
		if (jsonLotStatuses != null && !jsonLotStatuses.isEmpty()) {
		
			// build search condition from parameters in request
			Condition4LotStatus searchCriteria = SQLConditionBuilder.extractLotStatusParameter(request);
			
			// get template excel file which already pre-defined format and structure for content to populate
	    	InputStream is = this.getServlet().getServletContext().getResourceAsStream(EXCEL_TEMPLATE_FILE);
			
	    	// create duration color condition manager instance
			DurationColorConManager durColConManager = ManagerFactory.getInstance().getDurationColorConManager();
			
			// load durationColorCondition conditions
			DurationColorCon durColCon = durColConManager.getDuratonColCon();
	    	
			// get LotStatusManager instance
	    	LotStatusManager lotStatusManager = ManagerFactory.getInstance().getLotStatusManager();
	    	
	    	// query data and save the result to template excel file
	    	Workbook workbook = lotStatusManager.exportToExcel(searchCriteria, jsonLotStatuses, is, durColCon);
	    	
	    	// MIME type is excel
	        response.setContentType("application/vnd.ms-excel");
	        // offer the user the ability to download and save the resulting Excel file
	        response.setHeader("Content-Disposition", "attachment; filename=LotStatus.xls");
	    	ServletOutputStream out = response.getOutputStream();
	    	workbook.write(out);
	    	out.flush();
		}
    	LOGGER.debug("End LotStatusAction.exportExcel");
    	return null;
	}
	
	/**
	 * Response to export to pdf click, it will delegate to Manager class
	 * to query LotStatus according to search criteria in request parameter.
	 * The result list then will be save to pdf file and returns to client
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws JRException 
	 * @throws SQLException 
	 * @throws IOException 
	 * @throws ParseException 
	 */
	public ActionForward exportPdf(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException, JRException, IOException, ParseException 
			{
		LOGGER.debug("Begin LotStatusAction.exportPdf");
		
		String jsonLotStatuses = request.getParameter(JSON_DATA);
		
		if (jsonLotStatuses != null && !jsonLotStatuses.isEmpty()) {
			// build search condition from parameters in request
			Condition4LotStatus searchCriteria = SQLConditionBuilder.extractLotStatusParameter(request);
			
			// get LotStatusManager instance
	    	LotStatusManager lotStatusManager = ManagerFactory.getInstance().getLotStatusManager();
			
			JasperPrint jasperPrint = lotStatusManager.exportPdf(searchCriteria, jsonLotStatuses, this.getServlet().getServletContext()
					.getResourceAsStream(PDF_DESIGNER_FILE));
			
			// MIME type is pdf
	    	response.setContentType("application/pdf");
	    	// offer the user the ability to download and save the resulting pdf file
	    	response.setHeader("Content-Disposition", "attachment; filename=LotStatus.pdf");
			
			// stream out pdf content 
			ServletOutputStream servletOutputStream =response.getOutputStream();
	    	JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
	        servletOutputStream.flush();
		}
        LOGGER.debug("End LotStatusAction.exportPdf");
        return null;
	}	
	
}
